package com.ztewatch.wearcontacts.view;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentUris;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Groups;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ztewatch.wearcontacts.R;
import com.ztewatch.wearcontacts.data.IConfigLoader;
import com.ztewatch.wearcontacts.loader.SortContactsAndGroupLoader;
import com.ztewatch.wearcontacts.presenter.LoadCursorDataPresenter;
import com.ztewatch.wearcontacts.utils.ContactPhotoUtils;

public class ContactCursorAdapter extends CursorAdapter implements IConfigLoader{
	public static final int IS_CLICK_FLAG = R.id.is_click_flag;
	private Context mContext = null;
	private LayoutInflater mInflater;
	private boolean is_edit_flag = false;
	private boolean voice_call_flag = false;
	
	public ContactCursorAdapter(Context context, Cursor c) {
		super(context, c);
		mContext = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void setVoiceCallFlag(boolean isVoiceCall){
		voice_call_flag = isVoiceCall;
	}
	
	public void setEditActivityFlag(boolean isEdit){
		is_edit_flag = isEdit;
	}
	
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		return mInflater.inflate(R.layout.grid_item, parent, false);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		final TextView name_tv = (TextView) view.findViewById(R.id.item_text);
		final ImageView photo = (ImageView) view.findViewById(R.id.item_image);
		if (cursor != null) {
			int index = cursor.getColumnIndex(Contacts.DISPLAY_NAME_PRIMARY);
			int id_index = cursor.getColumnIndex(Contacts._ID);
			int id = 0;
			int is_group_index = cursor
					.getColumnIndex(SortContactsAndGroupLoader.IS_GROUP);
			if (index != -1 && id_index != -1) {
				String name = cursor.getString(index);
				id = cursor.getInt(id_index);
				name_tv.setText(name);
				int is_group_read_index = cursor
						.getColumnIndex(Groups.GROUP_IS_READ_ONLY);
				if (context.getResources().getString(R.string.history_str)
						.equals(name)
						&& id == 0 && !voice_call_flag) {
					name_tv.setEnabled(false);
					photo.setEnabled(false);
					view.setTag(ContactCursorAdapter.IS_CLICK_FLAG, false);
				} else if (is_group_read_index != -1 && is_edit_flag) {
					int group_read_flag = cursor.getInt(is_group_read_index);
					if (group_read_flag == 1) {
						name_tv.setEnabled(false);
						photo.setEnabled(false);
						view.setTag(ContactCursorAdapter.IS_CLICK_FLAG, false);
					} else {
						name_tv.setEnabled(true);
						photo.setEnabled(true);
						view.setTag(ContactCursorAdapter.IS_CLICK_FLAG, true);
					}
				} else {
					name_tv.setEnabled(true);
					photo.setEnabled(true);
					view.setTag(ContactCursorAdapter.IS_CLICK_FLAG, true);
				}

			} else {
				int index1 = cursor.getColumnIndex(Groups.TITLE);
				if (index1 != -1) {
					String name = cursor.getString(index1);
					name_tv.setText(name);
				}
				int is_group_read_index = cursor
						.getColumnIndex(Groups.GROUP_IS_READ_ONLY);
				if (is_group_read_index != -1) {
					int group_read_flag = cursor.getInt(is_group_read_index);
					if (group_read_flag == 1) {
						name_tv.setEnabled(false);
						photo.setEnabled(false);
						view.setTag(ContactCursorAdapter.IS_CLICK_FLAG, false);
					} else {
						name_tv.setEnabled(true);
						photo.setEnabled(true);
						view.setTag(ContactCursorAdapter.IS_CLICK_FLAG, true);;
					}
				} else {
					name_tv.setEnabled(true);
					photo.setEnabled(true);
					view.setTag(ContactCursorAdapter.IS_CLICK_FLAG, true);
				}
			}
			if (is_group_index != -1) {
				int is_group = cursor.getInt(is_group_index);
				if (is_group == 0) {
					bindContactPhoto(photo, cursor, id);
				} else {
					bindGroupPhoto(photo, cursor);
				}
			} else {
				bindContactPhoto(photo, cursor, id);
			}
		}
	}

	private void bindContactPhoto(ImageView photo, Cursor cursor, int id) {
		long photoId = 0;
		int index = cursor.getColumnIndex(Contacts.PHOTO_ID);
		if (index != -1) {
			if (!cursor.isNull(index)) {
				photoId = cursor.getLong(index);
			}
		}
		if (photoId != 0) {
			int id_index = cursor.getColumnIndex(Contacts._ID);
			if (id_index != -1) {
				Long contactID = cursor.getLong(id_index);
				Uri uri = ContentUris.withAppendedId(
						ContactsContract.Contacts.CONTENT_URI, contactID);
				InputStream input = ContactsContract.Contacts
						.openContactPhotoInputStream(
								mContext.getContentResolver(), uri);
				photo.setImageBitmap(BitmapFactory.decodeStream(input));
			}
		} else {
			if(id == 0){
				photo.setImageResource(R.drawable.ic_history);
			}else{
				photo.setImageResource(R.drawable.ic_address_portrait);
			}
		}
	}

	private void bindGroupPhoto(ImageView photo, Cursor cursor) {
		int photo_index = cursor.getColumnIndex(Contacts.LOOKUP_KEY);
		if (photo_index != -1) {
			byte[] photoarr = cursor.getBlob(photo_index);
			if (photoarr != null) {
				Bitmap bitmap = ContactPhotoUtils.Bytes2Bimap(photoarr);
				photo.setImageBitmap(bitmap);
			} else {
				photo.setImageResource(R.drawable.ic_friends);
			}
		} else {
			photo.setImageResource(R.drawable.ic_friends);
		}
	}

	public List<String> getLookUpKeys() {
		List<String> list = new ArrayList<String>();
		for (int index = 0; index < getCount(); index++) {
			Cursor cursor = (Cursor) getItem(index);
			if (cursor == null) {
				continue;
			}
			int lookup_key_index = cursor.getColumnIndex(Contacts.LOOKUP_KEY);
			if (lookup_key_index != -1) {
				String lookupKey = cursor.getString(lookup_key_index);
				list.add(lookupKey);
			}
		}
		return list;
	}

	@Override
	public CursorLoader getConfigLoader() {
		CursorLoader cursorLoader = new SortContactsAndGroupLoader(mContext);
		Uri uri = LoadCursorDataPresenter.CONTACT_AND_GROUP_AUTHORITY_URI;
		final Uri.Builder builder = uri.buildUpon();
		uri = builder.build();
		cursorLoader.setUri(uri);
		return cursorLoader;
	}
}
